<?php

namespace app\user\controller;

use app\user\model\UserModel;
use think\admin\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class User extends Controller
{
    /**
     * 当前操作数据库
     * @var string
     */
    private string $table = 'User';

    /**
     * 会员用户管理
     * @auth true
     * @menu true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $this->title = '会员用户管理';
        $query = $this->_query(UserModel::class);
        // 加载对应数据
        $this->typeOf = $this->request->get('typeOf', 'index');
        if ($this->typeOf == 'index') $query->where(['is_deleted' => 0]);
        elseif ($this->typeOf == 'is_deleted') $query->where(['is_deleted' => 1]);

        $query->like('userid,phone,username,type,sex')->order('id', 'DESC')->page(true, true);
    }

    public function detail()
    {
        $id = $this->request->get('id');
        $model = new UserModel();

        $userBase = $model->where(['id' => $id])->find();
        $userHouse = $model->getUserHouse()->where(['uid' => $id, 'status' => 1])->select()->toArray();
        $userPark = $model->getUserPark()->where(['uid' => $id, 'status' => 1])->select()->toArray();
        $userCar = $model->getUserCar()->where(['uid' => $id, 'status' => 1])->select()->toArray();
        $userFamily = $model->getUserFamily()->where(['uid' => $id, 'status' => 1])->select()->toArray();

        $data = [
            'userBase' => $userBase,
            'userHouse' => $userHouse,
            'userPark' => $userPark,
            'userCar' => $userCar,
            'userFamily' => $userFamily,
        ];

        $this->fetch('', $data);
    }
}